package com.mcxx.modules.temporary.controller;

import com.mcxx.modules.minimumliving.service.BankService;
import com.mcxx.modules.minimumliving.service.FundPayService;
import com.mcxx.modules.filemanage.easypoi.util.ExportExcelService;
import com.mcxx.modules.miniSalvation.family.dto.BankAbutmentDTO;
import com.mcxx.modules.miniSalvation.family.dto.MessageDTO;
import com.mcxx.modules.miniSalvation.family.dto.SpeedProgressDTO;
import com.mcxx.modules.miniSalvation.family.entity.BankEntity;
import com.mcxx.modules.miniSalvation.family.param.BankParam;
import com.mcxx.modules.miniSalvation.family.service.read.BankReadService;
import com.mcxx.modules.miniSalvation.family.service.write.ImportBankService;
import com.mcxx.modules.miniSalvation.fundpay.dto.ExcelBankDTO;
import com.mcxx.result.BaseResult;
import com.mcxx.service.DefaultRestService;
import com.mcxx.util.*;
import com.yinhai.ta404.component.audit.vo.ExcelInfo;
import com.yinhai.ta404.core.restservice.annotation.RestService;
import com.yinhai.ta404.core.restservice.requestbean.PageParam;
import com.yinhai.ta404.core.restservice.resultbean.Page;
import com.yinhai.ta404.core.validate.annotation.V;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.io.IOUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.MediaType;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestPart;
import org.springframework.web.multipart.MultipartFile;

import javax.servlet.http.HttpServletResponse;
import java.io.*;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;

@Slf4j
@Validated
@Api("临时救助待办列表")
@RestService({"temporary/bankManager/v2"})
public class TempBankManagerController extends DefaultRestService {

    @Autowired
    private BankService bankService;
    @Autowired
    private ImportBankService importBankService;
    @Autowired
    private ExportExcelService exportExcelService;
    @Autowired
    private FundPayService fundPayService;

    @Autowired
    private BankReadService bankReadService;

//    @Value("${society.template.dir}")


//    @Value("${society.template.bankTemplateName}")



    @PostMapping(value = "getBankPage")
    @ApiOperation(value = "银行账号按条件查询")
    public void queryBankAccountByCondition(BankParam param, PageParam pageParam) {
        param.setBusinessType(Constant.BusinessCode.LINSHI);
        Page page = bankService.getTempBankPage(getCurUserAccount(), param, pageParam);
        this.setPageBean(page);
    }


    @PostMapping(value = "getBankById")
    @ApiOperation(value = "根据银行账号ID查询银行账号信息")
    public void getBankById(String id) {
        log.info("根据id查询对应的银行卡信息，参数是:{}", id);
        BankEntity bankEntity = bankService.getBankById(id);
        setData(bankEntity);
        log.info("根据id查询对应的银行卡信息，结果是:{}", bankEntity);
    }

    @PostMapping(value = "getBankByFamilyId")
    @ApiOperation(value = "根据家庭id查询银行账号信息")
    public void getBankByFamilyId(String familyId,String memberId) {
        log.info("根据id查询对应的银行卡信息，参数是:{}", familyId);
        BankEntity bankEntity = bankService.getBankByFamilyId(familyId,memberId,Constant.BusinessCode.LINSHI);
        setData(bankEntity);
        log.info("根据id查询对应的银行卡信息，结果是:{}", bankEntity);
    }


    @PostMapping(value = "updateBankInfo")
    @ApiOperation(value = "维护救助对象的银行账号，保存更新")
    public void updateBankInfo(BankEntity  bankEntity) {
        log.info("维护救助对象的银行账号，保存更新，参数:{}", bankEntity);
        bankEntity.setBusinessType(Constant.BusinessCode.LINSHI);
        bankService.updateBankInfo(bankEntity,getCurUserAccount());
        log.info("维护救助对象的银行账号，保存更新，结果:{}", true);
        setData(true);
    }


    @PostMapping({"downLoadBankAccountTemplate"})
    public void exportOrgOpLog(HttpServletResponse response) throws IOException {
        List<ExcelBankDTO> list = new ArrayList<ExcelBankDTO>();
        ExcelBankDTO orgOpLogExcelEntity = new ExcelBankDTO();
        orgOpLogExcelEntity.setApplyname("哈哈");
        list.add(orgOpLogExcelEntity);
        ExcelInfo excelInfo = exportExcelService.exportOrgOpLogs(ExcelBankDTO.class,list,"bankTemplate.xls",
                "银行账号模板","银行账号模板",false,"");
        ZipInfoUtil.exportZipResponse(response, excelInfo, this.getCurUserId());
    }


    @PostMapping({"downLoadBankTemplate"})
    public void downLoadBankTemplate(HttpServletResponse response) throws IOException {
        log.info("下载银行账号开始");

        InputStream resourceAsStream = this.getClass().getResourceAsStream("/bakhtml/tempBankTemplate.xlsx");
        byte[] byt = new byte[resourceAsStream.available()];
        byt = IOUtils.toByteArray(resourceAsStream);

        log.info("二进制文件:{}", byt);
        ZipInfoUtil.exportZipByte(response,"bankTemplate.xlsx", byt, this.getCurUserId());
        log.info("下载银行账号结束");

    }



    @PostMapping(value = "importIntoBankAccount")
    @ApiOperation(value = "批量导入银行账号")
    public BaseResult<MessageDTO> importIntoBankAccount(@RequestPart("file") MultipartFile file) {
        if (CommonUtil.invalidArgs(file)) {
            setError(FailureCode.ERR_10000.getMessage());
        }
        BaseResult<MessageDTO> result = null;
        try {

            result = importBankService.importIntoBankAccountForTemp(file,file.getInputStream(),getCurUserAccount(), Constant.BusinessCode.LINSHI);
        } catch (IOException e) {
            e.printStackTrace();
        }
        return result;
    }


    @PostMapping(value = "getSpeedProgress")
    @ApiOperation(value = "查看导入进度")
    public void getSpeedProgress() {
        BaseResult<List<SpeedProgressDTO>> result = bankService.getSpeedProgress(Constant.BusinessCode.LINSHI,getCurUserAccount().getUserId());
        setData(result.getData());
    }


    @PostMapping("bankMemberIdCardCheck")
    @ApiOperation(value="银行卡代理人的身份证验证", produces = MediaType.APPLICATION_JSON_UTF8_VALUE,notes="银行卡代理人的身份证验证")
    public void bankMemberIdCardCheck(@V({"notnull"})  String accountIdcard){
        bankReadService.bankMemberIdCardCheck(accountIdcard);
    }


    @PostMapping({"downBankList"})
    public void downBankList(HttpServletResponse response,@RequestBody BankParam param) throws IOException {
        param.setBusinessType(Constant.BusinessCode.LINSHI);
        ExcelInfo excelInfo = bankService.exportBank(param,getCurUserAccount());
        if(null != excelInfo){
            FileExportUtil.exportFileResponse(response, excelInfo, getCurUserAccount().getAreaName());
//            ZipInfoUtil.exportZipResponse(response, excelInfo, this.getCurUserId());
        }
    }


    @GetMapping("gainBankInfo")
    @ApiOperation("获取财政银行卡信息")
    public void gainBankInfo(@Validated BankAbutmentDTO dto) {
        log.info("开始获取财政银行卡信息，参数是：{}", dto);
        Map<String, String> map = fundPayService.gainBankInfo(dto,getCurUserAccount());
        setData(map);

        log.info("获取财政银行卡信息结束");
    }
}
